//
//  KCHomeViewController.swift
//  MyKC
//
//  Created by yy on 2017/2/7.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit
import XHRealTimeBlur
import SVProgressHUD
import YYModel

// MARK: -  定义全局变量-----在另外一个控制器使用-----
var allNotice = [KCHomeNoticeModel]()

class KCHomeViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(tableView)
        tableView.estimatedRowHeight = 50
        tableView.rowHeight = UITableViewAutomaticDimension
        getData()
    }
    
    // MARK:
    // MARK: - 懒加载基本控件
    lazy var progressModel = KCHomeProgressModel()
    lazy var noticeContentModel = KCHomeNoticeContentModel()
    lazy var identifierArray = ["NoticeHeadCellId","KCSaleAnylseCellId","NoticeHeadCellId","KCAllFuncCellId"]
    
    //tableView-------
    lazy var tableView: UITableView = { [weak self] in
        let tableView = UITableView(frame: UIScreen.main.bounds)
        let footView = UIView()
        //注册xib
        tableView.register(UINib(nibName: "KCNoticeHeadCell", bundle: nil), forCellReuseIdentifier: "NoticeHeadCellId")
        tableView.register(UINib(nibName: "KCSaleAnylseCell", bundle: nil), forCellReuseIdentifier: "KCSaleAnylseCellId")
        tableView.register(UINib(nibName: "KCAllFuncCell", bundle: nil), forCellReuseIdentifier: "KCAllFuncCellId")
        tableView.register(UINib(nibName: "KCNoticetileCell", bundle: nil), forCellReuseIdentifier: "KCNoticetileCellId")
        tableView.tableFooterView = footView
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
        }()
}

// MARK:  -
// MARK:  table的数据源方法
extension KCHomeViewController: UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? 1 + allNotice.count : 1
    }
    
    // MARK: -  返回具体的cell----
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = getIdentifier(indexPath: indexPath)
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        setupCell(indexPath: indexPath, cell: cell)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? 0.1 : 10
    }
    
    // MARK: -  cell的点击事件---
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 && indexPath.row != 0 {
            SVProgressHUD.show()
            let model = allNotice[indexPath.row - 1]
            let dic = ["AnnouncementId":model.AnnouncementId]
            KCNetworkManager.sharedTools.request(method: .GET, urlString: "/api/Announcement/GetAnnouncement", parameters: dic, finished: { (response, error) in
                SVProgressHUD.dismiss()
                guard let responseObj = response as? NSDictionary,let responseContent = responseObj["Content"] as? NSDictionary else {
                    return
                }
//                self.noticeContentModel = KCHomeNoticeContentModel.yy_model(with: responseContent as! [AnyHashable : Any])!
                self.noticeContentModel.yy_modelSet(with: responseContent as! [AnyHashable : Any])
                let notice = KCNoticeDetailController(nibName: "KCNoticeDetailController", bundle: nil)
                notice.noticeContentModel = self.noticeContentModel                
                self.present(notice, animated: true, completion: nil)
            })
        }
    }
    
    private func getIdentifier(indexPath: IndexPath) -> String {
        if indexPath.section == 0 && indexPath.row != 0 {
            return "KCNoticetileCellId"
        }
        return identifierArray[indexPath.section]
    }
    
    private func setupCell(indexPath: IndexPath,cell: UITableViewCell)  {
        cell.selectionStyle = .none
        if indexPath.section == 0 && indexPath.row != 0 {
            let cellNoticeContent = cell as! KCNoticetileCell
            let model = allNotice[indexPath.row - 1]
            cellNoticeContent.lbe_notice.text = model.Title
        }
        else if indexPath.section == 1 {
            let cellAnyseCell = cell as! KCSaleAnylseCell
            cellAnyseCell.model = self.progressModel
        }
        else if indexPath.section == 2 {
            let cellNoticeHead = cell as! KCNoticeHeadCell
            cellNoticeHead.lbe_noticeHead.text = "我的工作台"
        }
        else if indexPath.section == 3 {
            let cellAllFunc = cell as! KCAllFuncCell
            cellAllFunc.delegate = self
        }
    }
}

// MARK: -  所有功能模块的点击事件
extension KCHomeViewController: KCAllFuncCellDelegate {
    func btnClick(tag: NSInteger) {
        tabBarController?.selectedIndex = tag / 10
    }
}


// MARK:
// MARK:  获取网络数据-------
extension KCHomeViewController {
    internal func getData() {
        SVProgressHUD.show()
        //开启队列调度组
        let group = DispatchGroup()
        group.enter()
        KCNetworkManager.sharedTools.request(method: .GET, urlString: "/api/Announcement/GetAnnouncementTitle", parameters: nil) { (response, error) in
            guard let responseObj = response as? NSDictionary, let responseContentArray = responseObj["Content"] as? NSArray else {
                group.leave()
                return
            }
            allNotice = NSArray.yy_modelArray(with: KCHomeNoticeModel.self, json: responseContentArray) as! [KCHomeNoticeModel]
            group.leave()

        }
        group.enter()
        KCNetworkManager.sharedTools.request(method: .GET, urlString: "/api/Task/GetSalesData", parameters: nil) { (response, error) in
            guard let responseObj = response as? NSDictionary,let responseContent = responseObj["Content"] as? NSDictionary else {
                group.leave()
                return
            }
            self.progressModel.yy_modelSet(with: responseContent as! [AnyHashable : Any])
            group.leave()
        }
        group.notify(queue: DispatchQueue.main) {
            SVProgressHUD.dismiss()
            self.tableView.reloadData()
        }
    }
}





